/* 
 *  Document   : sightData.js
 *  Created on : 2009年2月16日, 上午11:42
 *  Author     : xthsky
 *  Description:
 *      处理景点列表数据
 */
var Sight = {
	// host地址
	host: "http://localhost:8080/travel/",

	// 存储含有景点信息的xml
	$xml: undefined,

	/**
	 * 加载场景及列表
	 * 效果：加载并刷新场景列表及地图
	 * 
	 * @param name 待加载场景名称
	 */
	load: function(name) {
		$.get(Sight.host +"api/sight/"+ name, function(data) {
			Sight.$xml = $(data);
			var $sights = $("#sights");
			var $sight = Sight.$xml.find("sight");
			
			//index---start
			var html = "<ul class=\"index\">";
			$sight.find("index > *").each(function() {
				html += "<li onclick=\"location.href='"+Sight.host+"sight/"+encodeURIComponent($(this).find("name").text())+"'\">"+$(this).find("name").text()+"</li>";
			});
			html += "<li>" + $sight.find("> name").text() + "</li></ul>";
			//index---end
			
			//list---start
			html += "<ul class=\"list\">";
			$sight.find("spots > *").each(function() {
				html += "<li id=\"" + $(this).attr("mapId") + "\"";
				if ($(this).attr("url") != null) {
					html += " class=\"sight\"";
				}
				html += ">" + $(this).find("name").text() + "</li>";
			});
			html += "</ul>";
			//list---end
			
			$("#info").append($sight.find("description:eq(0)").text());

			$sights.html(html);
			//为新元素注册与景点相关的鼠标事件：单击、悬停、移出、双击等
			$sights.find(".list > li").click(Core.select).hover(Core.mouseover, Core.mouseout);
			$sights.find(".list > li.sight").dblclick(Core.dblclick);

			var map = $sight.attr("url");
			if(map != null) Map.loadScene("", "", "", "", map);
			
			//插入搜索景点元素,引用自searchPointData.js
			//addSearchPoint();
		});
	},

	//newPoint--start
	registerPoint: function() {
		Core.prepareLabel();
		$("#newPoint").val("保存").unbind("click", Sight.registerPoint).click(Sight.create);
	},
	create: function() {
		$("#info").text("creating...");
		$.post(Sight.host + "api/spot/",
				{	id:Core.selId,
					name:$("#name").val(),
					description:$("#description").val(),
					price:$("#price").val(),
					head:Sight.$xml.find("sight").attr("id"),
					xpos:Map.getItem(Core.selId).x,
					ypos:Map.getItem(Core.selId).y
				},
				function() {
			$("#info").text("create spot " + Core.selId);
		});
	},
	//newPoint--end
	
	//editPoint--start
	editPoint: function() {
		Core.editLabel();
		var person = {name:"John",location:"Boston"};
		$.ajax({
			type: "PUT",
			url: Sight.host + "api/spot/",
			//processData: false,
			data: JSON.stringify(person),
			success: function(msg){
				alert( "Data Saved: " + msg );
			}
		});
	},
	//editPoint--end
	
	//deletePoint--start
	deletePoint: function() {
		Core.prepareLabel();
		$.ajax({
			type: "DELETE",
			url: Sight.host + "api/spot/",
			success: function(msg){
				alert( "Data Saved: " + msg );
			}
		});
	}
};
